package com.magicode.portal.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.User;
import com.magicode.common.assist.PropertiesUtil;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.portal.service.ISecArticleService;
import com.magicode.portal.service.PermissionService;


@Service("secArticleService")
public class SecArticleServiceImpl implements ISecArticleService {
	
	@Autowired
	@Qualifier("portalpermissionService")
	private PermissionService permissionService;
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Override
	public Article get(String id) {
		String hql = " from com.magicode.cms.entiy.base.Article a "
				+ " left join fetch a.source "
				+ " left join fetch a.author "
				+ " left join fetch a.mainPic "
				+ " left join fetch a.tags "
				+" left join fetch a.channel "
				+ " where a.id=? ";
		Article article = null;
		try {
			article = (Article) this.commonDaoService.getByHQL(hql, new String[]{id});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return article;
	}

	@Override
	public void update(Article article) {
		try {
			this.commonDaoService.update(article);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Boolean isPermission(User user, Article article) {
		Boolean b = false;
		if(article !=null && article.getResumeCoin()!=null && article.getResumeCoin() > 0){
			if(article != null && user != null){
				b = permissionService.validVideoPermission(user.getId(), article.getId());
			}
		}else{
			b = true;
		}
		return b;
	}
	
	@Override
	public Long getArticleCount() {
		String hql = " select count(*) from com.magicode.cms.entiy.base.Article v ";
		Long count  = (Long) commonDaoService.getByHQL(hql, null);
		if(count!=null)
			return count;
		return 0L;
	}
	
}

